/*
 * Created by JFormDesigner on Mon Aug 13 20:06:56 EDT 2007
 */

package org.timeticker.gui;

import java.util.*;
import org.jdesktop.swingx.JXTable;
import org.timeticker.ProjectList;
import org.timeticker.Timekeeper;
import org.timeticker.Project;
import org.timeticker.impl.ProjectImpl;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * Projects dialog
 *
 * @author Ivan Latysh
 * @version 0.1
 * @since 13-Aug-2007 : 20:51:43 PM
 */
public class ProjectsDialog extends JDialog {

  /** Projects table model */
  private ProjectsTableModel projectsTableModel;

	public ProjectsDialog(Frame owner) {
		super(owner);
    preInit();
		initComponents();
    postInit();
	}

	public ProjectsDialog(Dialog owner) {
		super(owner);
    preInit();
		initComponents();
    postInit();
	}

  /**
   * Return selected project
   *
   * @return selected project or <tt>null</tt>
   */
  public Project getSelectedProject() {
    // get selected index
    int selected = projectsTable.getSelectedRow();
    // return null when nothing selected
    if (selected==-1) return null;
    // retrieve selected object
    return projectsTableModel.getProjectList().get(selected);
  }

  /**
   * Pre-Initialize dialog
   */
  protected void preInit() {
    // get ProjectList
    ProjectList projects = (ProjectList) Timekeeper.getInstance().getObjectList(Timekeeper.PROJECT_LIST);
    // create a new table model
    projectsTableModel = new ProjectsTableModel(projects);
  }

  /**
   * Post-Initialize dialog
   */
  protected void postInit() {
    //
  }

  private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - Ivan Latysh
		ResourceBundle bundle = ResourceBundle.getBundle("org.timeticker.timeticker");
		dialogPane = new JPanel();
		contentPanel = new JPanel();
		toolBar = new JToolBar();
		newBtn = new JButton();
		editBtn = new JButton();
		deleteBtn = new JButton();
		scrollPane1 = new JScrollPane();
		projectsTable = new JXTable(projectsTableModel);
		buttonBar = new JPanel();
		closeButton = new JButton(closeAction);
		newProjectAction = new NewProjectAction();
		editProjectAction = new EditProjectAction();
		deleteProjectAction = new DeleteProjectAction();
		closeAction = new CloseAction();

		//======== this ========
		setTitle(bundle.getString("ProjectsDialog.Title"));
		setModal(true);
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());

		//======== dialogPane ========
		{
			dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));

			// JFormDesigner evaluation mark
			dialogPane.setBorder(new javax.swing.border.CompoundBorder(
				new javax.swing.border.TitledBorder(new javax.swing.border.EmptyBorder(0, 0, 0, 0),
					"JFormDesigner Evaluation", javax.swing.border.TitledBorder.CENTER,
					javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("Dialog", java.awt.Font.BOLD, 12),
					java.awt.Color.red), dialogPane.getBorder())); dialogPane.addPropertyChangeListener(new java.beans.PropertyChangeListener(){public void propertyChange(java.beans.PropertyChangeEvent e){if("border".equals(e.getPropertyName()))throw new RuntimeException();}});

			dialogPane.setLayout(new BorderLayout());

			//======== contentPanel ========
			{
				contentPanel.setLayout(new BorderLayout());

				//======== toolBar ========
				{
					toolBar.setFloatable(false);

					//---- newBtn ----
					newBtn.setText("New");
					newBtn.setMaximumSize(new Dimension(70, 25));
					newBtn.setMinimumSize(new Dimension(70, 25));
					newBtn.setPreferredSize(new Dimension(70, 25));
					newBtn.setAction(newProjectAction);
					toolBar.add(newBtn);

					//---- editBtn ----
					editBtn.setText("Edit");
					editBtn.setPreferredSize(new Dimension(70, 25));
					editBtn.setMaximumSize(new Dimension(70, 25));
					editBtn.setMinimumSize(new Dimension(70, 25));
					editBtn.setAction(editProjectAction);
					toolBar.add(editBtn);

					//---- deleteBtn ----
					deleteBtn.setText("Delete");
					deleteBtn.setMaximumSize(new Dimension(70, 25));
					deleteBtn.setMinimumSize(new Dimension(70, 25));
					deleteBtn.setPreferredSize(new Dimension(70, 25));
					deleteBtn.setAction(deleteProjectAction);
					toolBar.add(deleteBtn);
				}
				contentPanel.add(toolBar, BorderLayout.NORTH);

				//======== scrollPane1 ========
				{
					scrollPane1.setViewportView(projectsTable);
				}
				contentPanel.add(scrollPane1, BorderLayout.CENTER);
			}
			dialogPane.add(contentPanel, BorderLayout.CENTER);

			//======== buttonBar ========
			{
				buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
				buttonBar.setLayout(new GridBagLayout());
				((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 80};
				((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0};

				//---- closeButton ----
				closeButton.setText(bundle.getString("Close"));
				closeButton.setAction(closeAction);
				buttonBar.add(closeButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
					GridBagConstraints.CENTER, GridBagConstraints.BOTH,
					new Insets(0, 0, 0, 0), 0, 0));
			}
			dialogPane.add(buttonBar, BorderLayout.SOUTH);
		}
		contentPane.add(dialogPane, BorderLayout.CENTER);
		pack();
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - Ivan Latysh
	private JPanel dialogPane;
	private JPanel contentPanel;
	private JToolBar toolBar;
	private JButton newBtn;
	private JButton editBtn;
	private JButton deleteBtn;
	private JScrollPane scrollPane1;
	private JXTable projectsTable;
	private JPanel buttonBar;
	private JButton closeButton;
	private NewProjectAction newProjectAction;
	private EditProjectAction editProjectAction;
	private DeleteProjectAction deleteProjectAction;
	private CloseAction closeAction;
	// JFormDesigner - End of variables declaration  //GEN-END:variables

	private class DeleteProjectAction extends AbstractAction {
		private DeleteProjectAction() {
			// JFormDesigner - Action initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
			// Generated using JFormDesigner Evaluation license - Ivan Latysh
			ResourceBundle bundle = ResourceBundle.getBundle("org.timeticker.timeticker");

			putValue(NAME, bundle.getString("ProjectsDialog.DeleteProjectAction.Title"));
			// JFormDesigner - End of action initialization  //GEN-END:initComponents
		}

		public void actionPerformed(ActionEvent e) {
      // get selected index
      int selectedindex = projectsTable.getSelectedRow();
      if (selectedindex!=-1) {
        // get selected project
        final Project project = projectsTableModel.getObjectAtRow(selectedindex);
        // delete project
        projectsTableModel.getProjectList().remove(project);
      }
		}
	}

	private class EditProjectAction extends AbstractAction {
		private EditProjectAction() {
			// JFormDesigner - Action initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
			// Generated using JFormDesigner Evaluation license - Ivan Latysh
			ResourceBundle bundle = ResourceBundle.getBundle("org.timeticker.timeticker");

			putValue(NAME, bundle.getString("ProjectsDialog.EditProjectAction.Title"));
			// JFormDesigner - End of action initialization  //GEN-END:initComponents
		}

		public void actionPerformed(ActionEvent e) {
      // get selected index
      int selectedindex = projectsTable.getSelectedRow();
      if (selectedindex!=-1) {
        // get selected project
        final Project project = projectsTableModel.getObjectAtRow(selectedindex);
        // create new project dialog
        ProjectDialog projectDialog = new ProjectDialog(ProjectsDialog.this, project);
        projectDialog.setVisible(true);
        // check result action
        if ("ok".equals(projectDialog.getAction())) {
          // add new project
          projectsTableModel.getProjectList().set(selectedindex, project);
        }
        // diapose dialog
        projectDialog.dispose();
      }			
		}
	}

	private class NewProjectAction extends AbstractAction {
		private NewProjectAction() {
			// JFormDesigner - Action initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
			// Generated using JFormDesigner Evaluation license - Ivan Latysh
			ResourceBundle bundle = ResourceBundle.getBundle("org.timeticker.timeticker");

			putValue(NAME, bundle.getString("ProjectsDialog.NewProjectAction.Title"));
			// JFormDesigner - End of action initialization  //GEN-END:initComponents
		}

		public void actionPerformed(ActionEvent e) {
      // create new project dialog
      final ProjectImpl newProject = new ProjectImpl();
      ProjectDialog projectDialog = new ProjectDialog(ProjectsDialog.this, newProject);
      projectDialog.setVisible(true);
      // check result action
      if ("ok".equals(projectDialog.getAction())) {
        // add new project
        projectsTableModel.getProjectList().add(newProject);
      }
      // diapose dialog
      projectDialog.dispose();
		}
	}

	private class CloseAction extends AbstractAction {
		private CloseAction() {
			// JFormDesigner - Action initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
			// Generated using JFormDesigner Evaluation license - Ivan Latysh
			ResourceBundle bundle = ResourceBundle.getBundle("org.timeticker.timeticker");

			putValue(NAME, bundle.getString("Close"));
			// JFormDesigner - End of action initialization  //GEN-END:initComponents
		}

		public void actionPerformed(ActionEvent e) {
			setVisible(false);
		}
	}
}
